package com.nebula.dao.impl;

import com.nebula.entity.RoleNavigation;
import com.nebula.dao.Database;
import com.nebula.dao.IRoleNavigationDao;
import com.nebula.util.DateTimeUtil;

import java.util.Date;
import java.util.List;

/**
 * @author CcccX
 * @create 2020-06-09 11:11
 */
public class RoleNavigationDaoImpl extends BaseDaoImpl<RoleNavigation> implements IRoleNavigationDao {

    @Override
    public int deleteByRoleId(int roleId) {
        String sql = "delete from role_navigation where role_id = ?";
        return (int) Database.excute(sql, new Object[]{(Object) roleId});
    }

    @Override
    public int deleteByNavId(String navId) {
        String sql = "delete from role_navigation where nav_id = ?";
        return (int) Database.excute(sql, navId);
    }

    @Override
    public int batchSave(List<RoleNavigation> roleNavigations) {
        StringBuilder tempSql = new StringBuilder("insert into role_navigation(nav_id,role_id,created_time," +
                "updated_time) values ");
        for (RoleNavigation roleNavigation : roleNavigations) {
            tempSql.append("( '").append(roleNavigation.getNavId()).append("', ").append(roleNavigation.getRoleId()).append(", '");
            if (roleNavigation.getCreatedTime() == null) {
                tempSql.append(DateTimeUtil.dateToStr(new Date()));
            } else {
                tempSql.append(DateTimeUtil.dateToStr(roleNavigation.getCreatedTime()));
            }

            tempSql.append("', '");

            if (roleNavigation.getUpdatedTime() == null) {
                tempSql.append(DateTimeUtil.dateToStr(new Date()));
            } else {
                tempSql.append(DateTimeUtil.dateToStr(roleNavigation.getUpdatedTime()));
            }

            tempSql.append("' ), ");
        }

        String sql = tempSql.toString();
        sql = sql.substring(0, sql.lastIndexOf(","));

        return (int) Database.excute(sql);
    }
}
